﻿/**
 * HTMLTextbox is the class for the HTML input box
*/
dynamic class HTMLTextbox extends MovieClip {
	
	// constants
	var padding:Number = 10;
	
	// variables
	var html_xml:XML;

	/**
	 * HTMLTextbox() instantiates the instance of the Text Box
	*/
	function HTMLTextbox() {
		
		// Initialise empty container
		this.html_xml = new XML("<HTMLTextBox display=\"block\"></HTMLTextBox>");

		// Load components
		this.attachMovie("element_background", "background_mc", this.getNextHighestDepth(), {
			useHandCursor: false
		});
		this.createTextField("html_txt", this.getNextHighestDepth(), 0, this.element_menu_mc._height, 100, 0);
		this.attachMovie("element_menu", "element_menu_mc", this.getNextHighestDepth());
		this.attachMovie("element_border", "element_border_mc", this.getNextHighestDepth());
		
		// Set Properties
		with (this.html_txt) {
			border = false;
			wordWrap = true;
			autoSize = true;
			multiline = true;
			html = false;
			type = "input";
			setNewTextFormat(_root.xhtml_text_tf);
			text = "Please only enter XHTML";
		}
		
		// Redraw layout when textfield changes
		this.html_txt.onChanged = function() {
			_parent._parent.drawLayout();
		};
			
		// When focusing on text, focus parent element
		this.html_txt.onSetFocus = function(oldFocus:Object) {
			if (_parent._parent.getElementFocus() != this._parent) {
				_parent._parent.setElementFocus(this._parent);
			}
		}

		// Make background clickable
		this.background_mc.onRelease = function() {
			_parent._parent.setElementFocus(_parent);
		}
	}
	
	/**
	 * redraw() redraws the HTML box
	 *
	 * @param column_width is the column width
	*/
	function redraw(column_width:Number):Void {
		
		// Editable text
		this.html_txt._width = column_width;
		this.html_txt._y = this.element_menu_mc._height;
		
		// Menu
		this.element_menu_mc.background_mc._width = column_width;
		
		// Background
		this.background_mc._width = column_width;
		this.background_mc._height = this.element_menu_mc._height + this.html_txt._height;
		
		// Border
		this.element_border_mc._width = column_width;
		this.element_border_mc._height = this.element_menu_mc._height + this.html_txt._height;

		// Make visible
		this._visible = true;
		
		// Update type
		this.displayDisplayType();
	}
	
	/**
	 * focusElement() focuses the lement
	*/
	function focusElement() {
		_root.focusElement(this);
	}
	
	/**
	 * blurElement() blurs the element
	*/
	function blurElement() {
		
		// Remove focus to link URL box
		_root.controls_mc.link_txt.text = "";
		Selection.setFocus(_root.controls_mc.link_txt);
		
		// Blur
		_root.blurElement(this);
	}

	/**
	 * setSource() sets the source XHTML
	 *
	 * @param html_string is the HTML to print
	*/
	function setSource(html_string) {
		
		// Store XML without translating tags - we only need the "Display" parameter
		this.html_xml = new XML(html_string);
		
		// Prepare formatted HTML - remove surrounding <htmlbox> tags
		var formatted_html = "" + html_string;
		formatted_html = formatted_html.substr(formatted_html.indexOf(">") + 1);
		formatted_html = formatted_html.substr(0, formatted_html.lastIndexOf("<"));
		
		// Convert from ASCII
		formatted_html = unescape(_root.replaceAll("@", "%", formatted_html));
		
		// Update text
		this.html_txt.text = formatted_html;

		// Redraw
		_parent.drawLayout();
	}
	
	/**
	 * getSource gets the XML source
	 *
	 * @return the source of this HTML box
	*/
	function getSource():String {
		
		// Get HTML
		var html_content:String = _root.toEscaped(this.html_txt.text);
		
		// Return
		return "<HTMLTextbox display=\"" + this.html_xml.firstChild.attributes.display + "\">" + html_content + "</HTMLTextbox>";
	}
	
	/**
	 * toggleDisplayType() toggles the display type
	*/
	function toggleDisplayType():Void {
		
		// If inline, swap to block, and vice versa
		if (this.html_xml.firstChild.attributes.display == "inline") {
			this.html_xml.firstChild.attributes.display = "block";
		} else {
			this.html_xml.firstChild.attributes.display = "inline";
		}
		
		// Update
		displayDisplayType();
	}

	/**
	 * displayDisplayType() updates the display type
	*/
	function displayDisplayType():Void {

		if (this.html_xml.firstChild.attributes.display == "inline") {
			
			// Change Checkbox
			this.element_menu_mc.inline_cb_mc.setCheckMode(0);
			
		} else {
			
			// Change Checkbox
			this.element_menu_mc.inline_cb_mc.setCheckMode(2);
		}
	}
	
	/**
	 * getMinimumWidth() returns 280
	 *
	 * @return 280
	*/
	function getMinimumWidth():Number {
		return 280;
	}
}